<?php

/**
 * Specify answer object. Answers table.
 */
class answer
{
	/**
	 * int answer id.
	 */
	var $id;
	
	/**
	 * int question id.
	 */
	var $question;
	
	/**
	 * int answer number.
	 */
	var $number;
	
	/**
	 * string answer text.
	 */
	var $text;
	
	/**
	 * int answer score.
	 */
	var $score;
	
	/**
	 * int (0, 1) answer is right.
	 */
	var $right;
	
	/**
	 * string answer text html.
	 */
	var $text_html;
	
	/**
	 * binary answer text.
	 */
	var $text_bin;
	
	/**
	 * int answer corresp.
	 */
	var $corresp;
	
	/**
	 * int next question number.
	 */
	var $nextq;
}

/**
 * Add answer.
 *
 * @param $p_answer answer class
 *
 * @return RecordSet or false
 */
function add_answer($p_answer)
{
	$t_answer_question_id = db_prepare_int($p_answer->question);
	$t_answer_number = db_prepare_int($p_answer->number);
	$t_answer_text = db_prepare_string($p_answer->text);
	$t_answer_score = db_prepare_int($p_answer->score);
	$t_answer_right = db_prepare_int($p_answer->right);
	$t_answer_text_html = db_prepare_string($p_answer->text_html);
	$t_answer_text_bin = ($p_answer->text_bin == NULL) ? 'NULL' : db_prepare_string($p_answer->text_bin);
	$t_answer_corresp = db_prepare_int($p_answer->corresp);
	$t_answer_nextq = db_prepare_int($p_answer->nextq);
	
	$query =  'INSERT INTO '.DB_TABLE_ANSWERS.'(
			`answer_question_id`,
			`answer_number`,
			`answer_text`,
			`answer_score`,
			`answer_right`,
			`answer_text_html`,
			`answer_text_bin`,
			`answer_corresp`,
			`answer_nextq`
			)
	    	  VALUES(
			'.$t_answer_question_id.',
			'.$t_answer_number.',
			'.$t_answer_text.',
			'.$t_answer_score.',
			'.$t_answer_right.',
			'.$t_answer_text_html.',
			'.$t_answer_text_bin.',
			'.$t_answer_corresp.',
			'.$t_answer_nextq.'
			)';
			
	db_exec($query);
}

/**
 * Delete answers for question id.
 *
 * @param $p_question_id int question id
 *
 * @return RecordSet or false
 */
function delete_answers_for_question_id($p_question_id)
{
	$t_question_id = db_prepare_int($p_question_id);
		
	$query = 'DELETE FROM '.DB_TABLE_ANSWERS.
		 ' WHERE `answer_question_id` = '.$t_question_id;
			
	db_exec($query);
}

/**
 * Get answers for question id.
 *
 * @param $p_question_id int question id
 *
 * @return array answers array
 */
function get_answers_for_question_id($p_question_id)
{
	$t_question_id = db_prepare_int($p_question_id);
		
	return db_extract(DB_TABLE_ANSWERS, 
			  '`answer_question_id` = '.$t_question_id,
			  '`answer_number` ASC'
			  );
}

/**
 * Get answer id.
 *
 * @param $p_answer_id int answer id
 *
 * @return answer class
 */
function get_answer($p_answer_id)
{
	$t_answer_id = db_prepare_int($p_answer_id);
		
	$answers[] = db_extract(DB_TABLE_ANSWERS, '`answer_id`='.$t_answer_id);
	$answer = new answer();
	if (count($answers[0]) > 0)
	{
		$answer->id = $answers[0][0]['answer_id'];
		$answer->question = $answers[0][0]['answer_question_id'];
		$answer->number = $answers[0][0]['answer_number'];
		$answer->text = $answers[0][0]['answer_text'];
		$answer->score = $answers[0][0]['answer_score'];
		$answer->right = $answers[0][0]['answer_right'];
		$answer->text_html = $answers[0][0]['answer_text_html'];
		$answer->text_bin = $answers[0][0]['answer_text_bin'];
		$answer->corresp = $answers[0][0]['answer_corresp'];
		$answer->nextq = $answers[0][0]['answer_nextq'];
	}
	
	return $answer;
}
